package com.ruoyi.personnel.management.staff.service;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.personnel.management.staff.domain.Staff;
import com.ruoyi.personnel.management.staff.domain.StaffLeaveRecord;
import com.ruoyi.personnel.management.staff.domain.StaffReinstateRecord;
import com.ruoyi.personnel.management.staff.domain.StaffTransferRecord;
import com.ruoyi.personnel.management.staff.mapper.StaffMapper;
import com.ruoyi.utils.UtilsAdd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static jdk.internal.dynalink.support.Guards.isNull;

/**
 * 员工信息
 * 业务层执行
 * @author Drj 2021/6/28
 */
@Service
public class StaffServiceImpl implements IStaffService {
    @Autowired
    private StaffMapper staffMapper;

    /**
     * 查询员工数组
     *
     * @param staff
     * @return
     */
    @Override
    public List<Staff> selectStaffList(Staff staff) {
        return staffMapper.selectStaffList(staff);
    }

    /**
     * 根据员工编号查询
     *
     * @param staffId 员工编号
     * @return
     */
    @Override
    public Staff selectStaffById(String staffId) {
        return staffMapper.selectStaffById(staffId);
    }

    /**
     * 新增员工
     *
     * @param staff 员工信息
     * @return
     */
    @Override
    public int insertStaff(Staff staff) {
        while (StringUtils.isNotNull(staffMapper.selectStaffById(staff.getStaffId()))){
            staff.setStaffId(UtilsAdd.addOne(staff.getStaffId(),1));
        }
        return staffMapper.insertStaff(staff);
    }

    /**
     * 更新员工
     *
     * @param staff 员工信息
     * @return
     */
    @Override
    public int updateStaff(Staff staff) {
        if (!StringUtils.isNotNull(staffMapper.selectStaffById(staff.getStaffId()))){
            System.out.println("1111111111111111111");
            return 0;
        }
        Staff staffFormer = staffMapper.selectStaffById(staff.getStaffId());
        System.out.println("staffFormer=================" + staffFormer);
        if (!staffFormer.getStaffStatus().equals(staff.getStaffStatus())){
            if (staff.getStaffStatus().equals("在职")){
                //新增复职
                staffMapper.insertStaffReinstateRecord(new StaffReinstateRecord(staff));
            }else {
                //新增离职
                staffMapper.insertStaffLeaveRecord(new StaffLeaveRecord(staff));
            }
        }
        if ((!staffFormer.getDepartmentId().equals(staff.getDepartmentId())) || (!staffFormer.getPositionInfoId().equals(staff.getPositionInfoId()))) {
            //新增人员调动记录
            staffMapper.insertStaffTransferRecord(new StaffTransferRecord(staffFormer,staff));
        }

        return staffMapper.updateStaff(staff);
    }

    /**
     * 删除员工
     * 仅修改员工删除标志
     *
     * @param staff
     * @return
     */
    @Override
    public int updateStaffDeleteFlag(Staff staff) {
        return staffMapper.updateStaffDeleteFlag(staff);
    }

    /**
     * 查询员工离职记录
     *
     * @param staffLeaveRecord
     * @return
     */
    @Override
    public List<StaffLeaveRecord> selectStaffLeaveRecordList(StaffLeaveRecord staffLeaveRecord) {
        return staffMapper.selectStaffLeaveRecordList(staffLeaveRecord);
    }

    /**
     * 查询员工复职记录
     *
     * @param staffReinstateRecord
     * @return
     */
    @Override
    public List<StaffReinstateRecord> selectStaffReinstateRecordList(StaffReinstateRecord staffReinstateRecord) {
        return staffMapper.selectStaffReinstateRecordList(staffReinstateRecord);
    }

    /**
     * 查询员工调动记录
     *
     * @param staffTransferRecord
     * @return
     */
    @Override
    public List<StaffTransferRecord> selectStaffTransferRecordList(StaffTransferRecord staffTransferRecord) {
        return staffMapper.selectStaffTransferRecordList(staffTransferRecord);
    }

    /**
     * 查询最新的员工
     * @return
     */
    @Override
    public Staff selectLatestStaff(){
        Staff staff = staffMapper.selectLatestStaff();
        return staff;
    }
}
